package com.cn.ccp.action.course;

import java.io.File;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseForumBean;
import com.cn.ccp.util.CCPContants;

/**
 * 课程论坛信息回复
 * @author Administrator
 *
 */
public class CourseForumReplyAction extends BaseAction{
	
	private Log log = LogFactory.getLog(CourseForumReplyAction.class);
	private List<CourseForumBean> list ;
	private CourseForumBean bean ;
	
	private String contents ;
	private String id ;
	private String replyInfo ;
	private String courseId,selectedCourseId;
	private String userImage;
	private boolean hasImage;
	
	
	public String showContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		String type = (String)context.getExternalContext().getRequestParameterMap().get("type");
		courseId = (String)context.getExternalContext().getRequestParameterMap().get("courseId");
		selectedCourseId = (String)context.getExternalContext().getRequestParameterMap().get("selectedCourseId");
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("bbs-type", type);
		bean = getCourseForumService().showBean(id);
		getCourseForumService().look(id);
		setCurrentPage(1);
		contents = "";
		
		StringBuffer buf = new StringBuffer();
		buf.append("/tempfile/");
		buf.append(bean.getUserId());
		buf.append("/");
		String imag = getUserHeadImage(bean.getUserId());
		if(imag == null){
			hasImage = false;
			userImage =  "images/teacher.png";
		}else{
			buf.append(imag);
			hasImage = true;
			userImage = buf.toString();
		}
		log.info(buf.toString());
		if(bean.getUserId().equals(CCPContants.getCurrUserId()))
			bean.setShowEdit(true);
		return "look";
	}
	
	public String reply(){
		FacesContext context = FacesContext.getCurrentInstance();
		contents = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1").toString();
		if(contents == null || contents.equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_2")));
			return "";
		}
		bean = getCourseForumService().showBean(id);
		getCourseForumService().install(bean.getCourseId() ,bean.getTitle(), contents, CCPContants.getCurrUserId() ,2 ,id);
		getCourseForumService().reply(id);
		
		
		contents = "";
		context.addMessage("formid", new FacesMessage(getMessages("label_success")));
		getpagedetail();
		return "";
	}
	
	public String goback(){
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String formPage = (String) session.getAttribute("bbs-type");
		if(formPage != null  && !formPage.equals("")){
			return formPage;
		}
		return "main";
	}
	
	
	private String getUserHeadImage(String userId){
		File des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+ userId);
		String image = null;
		if(des.isDirectory()){
			File[] files = des.listFiles();
			for(File f : files){
				if(f.getName().indexOf("head-image") != -1){
					image = f.getName();
					break;
				}
			}
		}
		log.info(image);
		return image;
	}
	
	
	@Override
	public void getpagedetail() {
		setItemCount(getCourseForumService().getReplyCount(id));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		list = getCourseForumService().replyPageQuery(start, pagesize ,id);
		
		replyInfo = "";
		StringBuffer buf = new StringBuffer();
		if(list == null || list.size() == 0){
		}else{
			for (int i = 0; i < list.size(); i++) {
				CourseForumBean bean = list.get(i);
				
				buf.append("<div style='margin-left:5px'>");
				buf.append("<div >");
				String image = getUserHeadImage(bean.getUserId());
				if(image == null)
					buf.append("<img style='float:left;height:77px;width:58px'  src='images/teacher.png'/>");
				else
					buf.append("<img style='float:left;height:77px;width:58px'  src='tempfile/"+bean.getUserId()+"/"+image+"' /'>");
	             	
				buf.append("</div>");
				buf.append("<div style='margin-left:70px' ><div><div style='margin-left:10px' >");
				buf.append("<h5>");
				buf.append(bean.getUserName());
				buf.append(": <small>"+bean.getCreateTime()+"</small>");
				buf.append("<p> ");
				buf.append(bean.getContents());
				buf.append("</p>");
				buf.append("</h5>");	
				buf.append("<br/></div></div></div>");
				buf.append("</div><hr  style='margin-top:10px;border-bottom: 1px solid rgba(156, 164, 173, 0.63)' />");
				
			}
		}
		replyInfo = buf.toString();
	}
	

	public List<CourseForumBean> getList() {
		getpagedetail();
		return list;
	}

	public void setList(List<CourseForumBean> list) {
		this.list = list;
	}

	public CourseForumBean getBean() {
		if(id != null && !id.equals("")){
			bean = getCourseForumService().showBean(id);
			if(bean.getUserId().equals(CCPContants.getCurrUserId()))
				bean.setShowEdit(true);
		}
		return bean;
	}

	public void setBean(CourseForumBean bean) {
		this.bean = bean;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}

	public String getReplyInfo() {
		getpagedetail();
		return replyInfo;
	}

	public void setReplyInfo(String replyInfo) {
		this.replyInfo = replyInfo;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getSelectedCourseId() {
		return selectedCourseId;
	}

	public void setSelectedCourseId(String selectedCourseId) {
		this.selectedCourseId = selectedCourseId;
	}

	public String getUserImage() {
		return userImage;
	}

	public void setUserImage(String userImage) {
		this.userImage = userImage;
	}

	public boolean isHasImage() {
		return hasImage;
	}

	public void setHasImage(boolean hasImage) {
		this.hasImage = hasImage;
	}
	
}
